package org.jeecg.modules.user.notice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.api.MemberAPI;
import org.jeecg.common.api.dto.MemberDTO;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.system.send.entity.BikeAnnouncementSend;
import org.jeecg.modules.system.send.mapper.BikeAnnouncementSendMapper;
import org.jeecg.modules.user.notice.entity.Notice;
import org.jeecg.modules.user.notice.mapper.NoticeMapper;
import org.jeecg.modules.user.notice.mapper.NoticeSendMapper;
import org.jeecg.modules.user.notice.service.NoticeService;
import org.jeecg.modules.utils.BikeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Description: bike_pile
 * @Author: jeecg-boot
 * @Date: 2021-05-19
 * @Version: V1.0
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeSendMapper noticeSendMapper;
    @Resource
    private BikeAnnouncementSendMapper bikeAnnouncementSendMapper;

    @Autowired
    private MemberAPI memberAPI;


    @Override
    public BikeResult getNoticeList(Integer type, String id, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "titile", "description", "start_time", "end_time", "send_time");
        if (StringUtil.isNotEmpty(id)) {
            queryWrapper.select("msg_content");
            queryWrapper.eq("id", id);
        } else {
            queryWrapper.eq("msg_category", type);
        }
        queryWrapper.eq("send_status", "1");
        queryWrapper.eq("del_flag", "0");
        queryWrapper.orderByDesc("is_top", "create_time");
        Page<Notice> userPage = new Page<>(pageNum, pageSize, true);
        IPage<Notice> iPage = noticeMapper.selectPage(userPage, queryWrapper);
        return BikeResult.ok("1", "查询成功", iPage);
    }


    @Override
    public BikeResult getIconCount(HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        MemberDTO member = memberAPI.getMember(username);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("read_flag", "0");
        qw.eq("user_id", member.getId());
        int count = noticeSendMapper.selectCount(qw);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);

        return BikeResult.ok("1", "成功", jsonObject);
    }

    @Override
    public BikeResult getSysMessage(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        //获取用户信息
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        MemberDTO member = memberAPI.getMember(username);
        //分页查询 公共系统消息和个人系统消息
        Page<Notice> page = new Page<>(pageNum, pageSize, true);
        IPage<Notice> noticeIPage = noticeMapper.noticePage(page, member.getId());

        BikeAnnouncementSend bikeAnnouncementSend = new BikeAnnouncementSend();
        bikeAnnouncementSend.setReadFlag("1");
        bikeAnnouncementSend.setReadTime(new Date());
        bikeAnnouncementSendMapper.update(bikeAnnouncementSend, new QueryWrapper<BikeAnnouncementSend>().eq("user_id", member.getId()));
        return BikeResult.ok("1", "成功", noticeIPage);
    }

}
